package com.java502.shirodemo502.controller;

import com.java502.shirodemo502.pojo.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

/**
 * @author:Teacher黄
 * @date:Created at 2019/12/30
 */
@Controller
public class UserController {


    /**
     * 登陆页面
     * @return
     */
    @GetMapping("/user/login_page")
    public String loginPage(){
        // 用户登陆的页面
        return "user/login";
    }


    /**
     * 登陆的方法
     * @param user
     * @return
     */
    @PostMapping("/user/login")
    public String login(User user){
        System.out.println(user);
        // 获取subject
        Subject subject = SecurityUtils.getSubject();
        // 准备token
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserName(), user.getUserPwd());
        // 设置记住我
        token.setRememberMe(user.isRememberMe());
        // 登陆
        subject.login(token);
        return "redirect:/index";
    }



}
